package com.ah.shanxi_demo.controller.web;

import com.ah.shanxi_demo.entity.SxOrderReceipt;
import com.ah.shanxi_demo.param.input.OrderPO;
import com.ah.shanxi_demo.param.result.OrderGeneralRO;
import com.ah.shanxi_demo.param.result.OrderRO;
import com.ah.shanxi_demo.param.result.OrderShowRO;
import com.ah.shanxi_demo.param.result.SXResult;
import com.ah.shanxi_demo.service.impl.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.ah.shanxi_demo.convert.ConvertOrder.convert_order_ro_list;

/**
 * @author zt 2020/8/14
 */
@RestController
@Api(value = "陕西高速", description = "陕西高速-工单数据服务")
@RequestMapping(value = "/api/v1/shanxi/order/")
public class ApiOrderController {

    private static final Logger log = LoggerFactory.getLogger(ApiOrderController.class);

    @Autowired
    private OrderService orderService;


    @ApiOperation(value = "工单列表查询", notes = "工单列表查询")
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public SXResult<List<OrderRO>> orderList(@RequestBody OrderPO orderPO) {
        try {
            List<OrderRO> sxOrderList = convert_order_ro_list(orderService.listBySelect(orderPO));
            Integer total = orderService.listCountBySelect(orderPO);
            return SXResult.instance(sxOrderList, total);
        } catch (Throwable e) {
            log.error("ApiOrderController 【list】" + e.getMessage());
        }
        return SXResult.instance();
    }

    @ApiOperation(value = "工单批量删除", notes = "策略批量删除")
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public SXResult<Boolean> orderDelete(@RequestBody Long[] orderIdList) {

        try {
            Boolean deleteBoolean = orderService.deleteOrder(orderIdList);
            return SXResult.instance(deleteBoolean);
        } catch (Throwable e) {
            log.error("ApiOrderController 【delete】" + e.getMessage());
        }
        return SXResult.instance();
    }


    @ApiOperation(value = "策略概览数据", notes = "策略查看")
    @RequestMapping(value = "general", method = RequestMethod.POST)
    public SXResult<OrderGeneralRO> general() {

        try {
            OrderGeneralRO general = orderService.general();
            return SXResult.instance(general);
        } catch (Throwable e) {
            log.error("ApiOrderController 【general】" + e.getMessage());
        }
        return SXResult.instance();
    }

    @ApiOperation(value = "工单回执查看", notes = "工单回执查看")
    @RequestMapping(value = "show", method = RequestMethod.POST)
    public SXResult<OrderShowRO> orderShow(@RequestBody Long orderId) {

        try {
            SxOrderReceipt orderReceipt = orderService.orderShow(orderId);
            return SXResult.instance(orderReceipt);
        } catch (Throwable e) {
            log.error("ApiOrderController 【show】" + e.getMessage());
        }
        return SXResult.instance();
    }

}
